#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <list>
#include <map>
#include <cstring>
#include <string>

#include "player.h"

class ISlotInterface;

extern double packetCount;

class protocol
{
 public :

  struct	NetHeader
  {
    unsigned char	_slotType;
    unsigned short	_packetId;
    unsigned short	_len;
  };

  struct	NetPacket
  {
    NetHeader		_header;
    char		data[255];
  }__attribute__((packed));

  enum SlotType
  {
    OK = 1,
    KO,
    PLAYER_PSEUDO,
    CHOSE_MAP,
    JOIN_GAME,
    LEFT_GAME,
    READY,
    LAUNCH_GAME,
    GAME_PORT,
    FINISH_GAME,
    OBJ_DEST,
    OBJ_INF,
    USER_EVENT,
    PING,
    IP
  };

  void			readEvent(Player *);
  void			onReceivePacket(Player *);
  void			dispatchSlots(Player *, void *);
  
  void			sendPacket(Player *);
  void			send(Player *player, SlotType type, std::string const &login, const char *data, unsigned int len, unsigned int id);
  void			send(Player *player, SlotType type, std::string const &login, const char *data, unsigned int len);

  void			registerSlot(SlotType, ISlotInterface *);

  protocol();
  ~protocol();

 private :

  NetHeader					_header;
  char					       *_buffer;
  std::map<SlotType, ISlotInterface *>		_slotMap;
};

#endif
